/**
  ******************************************************************************
  * @file    platform.c
  * @author  Iron
  * @date    2022-01-01
  * @version v1.0
  * @brief   platform c file
  */

/* Private includes ----------------------------------------------------------*/
#include "contiki.h"
#include "lib/random.h"
#include "net/linkaddr.h"
#include "dev/serial-line.h"

/*---------------------------------------------------------------------------*/
/* Log configuration */
#include "sys/log.h"
#define LOG_MODULE  "PLATFORM"
#define LOG_LEVEL   LOG_LEVEL_MAIN

/* Private define ------------------------------------------------------------*/
//#define UART_CONF_ENABLE    1

/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint32_t SystemCoreClock;

/* Private function prototypes -----------------------------------------------*/

/*---------------------------------------------------------------------------*/
void
platform_init_stage_one(void)
{
    /* PLL上电 */
    PWR_UnitModCfg(ENABLE, UNIT_SYS_PLL);
    DelayMs(3);

    /* 外部晶振 PLL 输出32MHz */
    SetSysClock(CLK_SOURCE_HSE_32MHz);

    /* Configure the system clock */
    SystemCoreClock = GetSysClock();

//  leds_init();
//  fade(LEDS_YELLOW);
}

/*---------------------------------------------------------------------------*/
void
platform_init_stage_two()
{
    /*
     * Character I/O Initialisation.
     * When the UART receives a character it will call serial_line_input_byte to
     * notify the core. The same applies for the USB driver.
     *
     * If slip-arch is also linked in afterwards (e.g. if we are a border router)
     * it will overwrite one of the two peripheral input callbacks. Characters
     * received over the relevant peripheral will be handled by
     * slip_input_byte instead
     */
    bsp_init();

    serial_line_init();

    /* Initialise the H/W RNG engine. */
    random_init(0);

//    udma_init();

#if CRYPTO_CONF_INIT
    crypto_init();
    crypto_disable();
#endif

    /* Populate linkaddr_node_addr */
#if NETSTACK_CONF_WITH_IPV4 || NETSTACK_CONF_WITH_IPV6
    {
#if LINKADDR_SIZE == 8
        GetUniqueID(linkaddr_node_addr.u8);
#endif
#if LINKADDR_SIZE == 6
        GetMACAddress(linkaddr_node_addr.u8);
#endif
//        ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE);
    }
#endif

//  button_hal_init();
//
//  INTERRUPTS_ENABLE();
//
//  fade(LEDS_GREEN);
}

/*---------------------------------------------------------------------------*/
void
platform_init_stage_three()
{
//    LOG_INFO("%s\n", CONTIKI_BOARD_STRING);

//  set_rf_params();
//
//  soc_print_info();
//
//  adc_init();
//
//  process_start(&sensors_process, NULL);
//
//  fade(LEDS_ORANGE);
}

/*---------------------------------------------------------------------------*/
void
platform_idle()
{
    /* We have serviced all pending events. Enter a Low-Power mode. */
//  lpm_enter();
}


/**
  * @}
  */

/******************* (C)COPYRIGHT 2022 ***** END OF FILE *********************/
